﻿using HandyControl.Controls;
using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 三维转台控制系统.Controll
{
    /// <summary>
    /// SelectFile.xaml 的交互逻辑
    /// </summary>
    public class SelectPathTextBox : System.Windows.Controls.TextBox
    {
        public static readonly DependencyProperty SelectPathCommandProperty = DependencyProperty.Register("SelectPathCommand", typeof(ICommand), typeof(SelectPathTextBox));

        //private ICommand? _selectPathCommand;
        public ICommand SelectPathCommand
        {
            get { return (ICommand)GetValue(SelectPathCommandProperty); }
            set { SetValue(SelectPathCommandProperty, value); }
        }



        public bool IsFolderPicker
        {
            get { return (bool)GetValue(IsFolderPickerProperty); }
            set { SetValue(IsFolderPickerProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsFolderPicker.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsFolderPickerProperty =
            DependencyProperty.Register("IsFolderPicker", typeof(bool), typeof(SelectPathTextBox), new PropertyMetadata(false));




        public bool MultiSelect
        {
            get { return (bool)GetValue(MultiSelectProperty); }
            set { SetValue(MultiSelectProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MultiSelect.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MultiSelectProperty =
            DependencyProperty.Register("MultiSelect", typeof(bool), typeof(SelectPathTextBox), new PropertyMetadata(false));






        public SelectPathTextBox()
        {
            SelectPathCommand = new DelegateCommand<object>((object obj) =>
            {
                string fullFileName = this.Text;

                CommonOpenFileDialog dialog = new CommonOpenFileDialog();
                //dialog.InitialDirectory = fullFileName;
                if (IsFolderPicker)
                {
                    dialog.IsFolderPicker = true;
                }

                if (MultiSelect)
                {
                    dialog.Multiselect = true;
                    if (dialog.ShowDialog(GetTopWindow()) != CommonFileDialogResult.Ok)
                        return;

                    fullFileName = "";

                    if (dialog.FileNames != null)
                    {
                        if (dialog.FileNames.ToList().Count == 1)
                        {
                            fullFileName = dialog.FileNames.First();
                        }
                        else if (dialog.FileNames.ToList().Count > 1)
                        {
                            for (int i = 0; i < dialog.FileNames.ToList().Count; i++)
                            {
                                fullFileName += dialog.FileNames.ToList()[i];
                                if (i == dialog.FileNames.ToList().Count - 1)
                                {
                                    break;
                                }
                                fullFileName += "\n";
                            }
                        }
                        else
                        {

                        }
                    }
                }
                else
                {
                    if (dialog.ShowDialog(GetTopWindow()) != CommonFileDialogResult.Ok)
                        return;

                    fullFileName = dialog.FileName;
                }

                SetCurrentValue(TextProperty, fullFileName);
            });
        }
        //获取Top窗口
        [DllImport("user32.dll")]
        static extern IntPtr GetActiveWindow();
        public static System.Windows.Window? GetTopWindow()
        {
            var hwnd = GetActiveWindow();
            if (hwnd == IntPtr.Zero)
                return Application.Current.MainWindow;
            return GetWindowFromHwnd(hwnd);
        }

        private static System.Windows.Window? GetWindowFromHwnd(IntPtr hwnd)
        {
            var fromHwnd = HwndSource.FromHwnd(hwnd);
            if (fromHwnd != null)
                return (System.Windows.Window)fromHwnd.RootVisual;
            return null;
        }
    }

    internal class DelegateCommand<T> : ICommand
    {
        public DelegateCommand(Action<object> value)
        {
            DoExecute = value;
        }

        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter)
        {
            return true;
        }

        public void Execute(object? parameter)
        {
            DoExecute?.Invoke(parameter);

        }
        public Action<object> DoExecute { get; set; }

    }
}
